<template>
  <div class="customers-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>客户管理</h2>
        <p>管理中老年旅游客户档案</p>
      </div>
      <div class="header-right">
        <el-button type="primary" @click="router.push('/customers/create')">
          <el-icon><Plus /></el-icon>
          新增客户
        </el-button>
      </div>
    </div>
    
    <!-- 搜索和筛选 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="searchForm" inline>
        <el-form-item label="搜索">
          <el-input
            v-model="searchForm.keyword"
            placeholder="客户姓名、手机号、客户编号"
            style="width: 200px"
            clearable
            @clear="handleSearch"
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.status"
            placeholder="客户状态"
            style="width: 120px"
            clearable
            @change="handleSearch"
          >
            <el-option label="全部" value="" />
            <el-option label="活跃" value="active" />
            <el-option label="非活跃" value="inactive" />
            <el-option label="潜在" value="potential" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 客户列表 -->
    <el-card class="table-card" shadow="never">
      <el-table
        :data="customers"
        :loading="loading"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        
        <el-table-column prop="customer_number" label="客户编号" width="120" />
        
        <el-table-column prop="name" label="姓名" width="100" />
        
        <el-table-column prop="gender" label="性别" width="60" />
        
        <el-table-column prop="age" label="年龄" width="60" />
        
        <el-table-column prop="phone" label="手机号" width="130" />
        
        <el-table-column prop="email" label="邮箱" min-width="150" show-overflow-tooltip />
        
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag
              :type="getStatusType(row.status)"
              size="small"
            >
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="created_at" label="创建时间" width="160">
          <template #default="{ row }">
            {{ dayjs(row.created_at).format('YYYY-MM-DD HH:mm') }}
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="viewCustomer(row.id)">
              查看
            </el-button>
            <el-button link type="primary" @click="editCustomer(row.id)">
              编辑
            </el-button>
            <el-button link type="danger" @click="deleteCustomer(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'

const router = useRouter()

// 加载状态
const loading = ref(false)

// 搜索表单
const searchForm = reactive({
  keyword: '',
  status: ''
})

// 分页信息
const pagination = reactive({
  page: 1,
  pageSize: 20,
  total: 0
})

// 客户列表
const customers = ref([
  {
    id: 1,
    customer_number: 'CU000001',
    name: '张三',
    gender: '男',
    age: 65,
    phone: '13800138001',
    email: 'zhangsan@example.com',
    status: 'active',
    created_at: '2024-01-01T10:00:00'
  },
  {
    id: 2,
    customer_number: 'CU000002',
    name: '李四',
    gender: '女',
    age: 58,
    phone: '13800138002',
    email: 'lisi@example.com',
    status: 'inactive',
    created_at: '2024-01-02T10:00:00'
  }
])

// 选中的客户
const selectedCustomers = ref([])

// 组件挂载时
onMounted(() => {
  loadCustomers()
})

// 加载客户列表
const loadCustomers = async () => {
  loading.value = true
  
  try {
    // TODO: 调用API获取客户列表
    console.log('加载客户列表', { 
      ...searchForm, 
      page: pagination.page, 
      pageSize: pagination.pageSize 
    })
    
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 设置分页信息
    pagination.total = 2
    
  } catch (error) {
    console.error('加载客户列表失败:', error)
    ElMessage.error('加载客户列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadCustomers()
}

// 重置搜索
const resetSearch = () => {
  searchForm.keyword = ''
  searchForm.status = ''
  pagination.page = 1
  loadCustomers()
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.page = 1
  loadCustomers()
}

// 当前页改变
const handleCurrentChange = (page: number) => {
  pagination.page = page
  loadCustomers()
}

// 选择改变
const handleSelectionChange = (selection: any[]) => {
  selectedCustomers.value = selection
}

// 查看客户
const viewCustomer = (id: number) => {
  router.push(`/customers/${id}`)
}

// 编辑客户
const editCustomer = (id: number) => {
  router.push(`/customers/${id}/edit`)
}

// 删除客户
const deleteCustomer = async (id: number) => {
  try {
    await ElMessageBox.confirm(
      '确定要删除这个客户吗？删除后不可恢复。',
      '删除确认',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // TODO: 调用删除API
    ElMessage.success('客户删除成功')
    loadCustomers()
    
  } catch (error) {
    // 用户取消了操作
  }
}

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap = {
    active: 'success',
    inactive: 'warning',
    potential: 'info'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap = {
    active: '活跃',
    inactive: '非活跃',
    potential: '潜在'
  }
  return statusMap[status] || '未知'
}
</script>

<style scoped>
.customers-page {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
}

.header-left h2 {
  margin: 0 0 5px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.header-left p {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.search-card {
  margin-bottom: 20px;
  border: none;
}

.table-card {
  border: none;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 15px;
  }
  
  .header-right {
    align-self: flex-start;
  }
  
  .el-form--inline .el-form-item {
    display: block;
    margin-bottom: 15px;
  }
  
  .pagination-container {
    justify-content: center;
  }
  
  .el-pagination {
    flex-wrap: wrap;
  }
}
</style>